﻿using System;
using System.Windows.Data;

namespace Rio.Framework.Pages.Helpers.Converter
{
    public class IndexValueConverter : IValueConverter
    {

        object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null && value.GetType() == typeof(string))
            {
                string L_szValue = (string)value;

                string[] L_oTabValue = L_szValue.Split(new char[] { ';' });
                //For every value from ListValues, we have "VALUE;ID" for value list type index and user list type index, and we have "VALUE" for the other index type
                //because for value list type index and user list type index we need the value to display and id to identify the value (for the consultation screen for exemple, to select the item in the combobox) 
                //So here we display only the Value 
                if (L_oTabValue != null)
                {
                    int l_oId;
                    if (L_oTabValue.Length == 2 && int.TryParse(L_oTabValue[1], out l_oId))
                    {
                        return L_oTabValue[0];
                    }
                    else
                    {
                        return L_szValue;
                    }
                }
            }
            return value;
        }

        object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
